package model

import (
	"context"
	sq "github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ StoreTypeModel = (*customStoreTypeModel)(nil)

type (
	// StoreTypeModel is an interface to be customized, add more methods here,
	// and implement the added methods in customStoreTypeModel.
	StoreTypeModel interface {
		storeTypeModel
		FindListByPlatform(ctx context.Context, platform string) ([]*StoreType, error)
	}

	customStoreTypeModel struct {
		*defaultStoreTypeModel
	}
)

// NewStoreTypeModel returns a model for the database table.
func NewStoreTypeModel(conn sqlx.SqlConn, c cache.CacheConf) StoreTypeModel {
	return &customStoreTypeModel{
		defaultStoreTypeModel: newStoreTypeModel(conn, c),
	}
}

func (m *defaultStoreTypeModel) FindListByPlatform(ctx context.Context, platform string) ([]*StoreType, error) {
	query, args, err := sq.Select(storeTypeRows).From(m.table).
		Where(sq.Eq{"platform": platform, "del_flag": false}).PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*StoreType
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, args...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return resp, nil
	default:
		return nil, err
	}
}
